\documentclass[t,12pt,aspectratio=169]{beamer} % 16:9 宽屏比例，适合现代投影
\usepackage{ctex} % 中文支持
\usepackage{amsmath, amssymb} % 数学公式与符号
\usepackage{graphicx}
\usepackage{url}
\usepackage{verbatim}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 插入代码
\usepackage{listings}
\usepackage{color}

% 设置列表的样式
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2
}

\lstset{style=mystyle}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 主题设置（推荐简洁风格）
\usetheme{Madrid}
\usecolortheme{default} % 可选：seahorse, beaver, dolphin 等

\title{R语言统计入门第3章：概率与分布}
\author{PD ET AL}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\begin{frame}
  \titlepage
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{目录 Probability and distributions}

\begin{enumerate}
\item[3.1.] 随机抽样 Random sampling 
\item[3.2.] 概率计算和排列组合 Probability calculations and combinatorics
\item[3.3.] 离散分布 Discrete distributions
\item[3.4.] 连续分布 Continuous distributions
\item[3.5.] 内置函数：The built-in distributions in R
\item[3.6.] 书中习题 Exercises 
\item[3.7.] 练习（选择题）
\item[3.8.] 练习（简答题）
\end{enumerate}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{课程讲解重点难点 }

\begin{enumerate}

\item  随机抽样的模拟，计算排列组合数和概率。 
\item  概率分布：二项分布、泊松分布、均匀分布、正态分布、指数分布。
\item  统计分布：$t$分布、$F$分布、$\chi^2$分布。
\item  对每种分布，计算密度函数、分布函数、分位数和随机数。

\end{enumerate}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.1.a. 随机抽样 Random sampling}

\begin{itemize}
\item {\color{red}Q: Pick five numbers at random from the set $\{1,2,\cdots,40\}$. }
\item 解答：这是无放回的抽样，
\begin{lstlisting}[language=R]
sample(1:40,5)
sample(40,5)
\end{lstlisting}

\item {\color{red}问题：模拟扔10次硬币，写出一种结果。Simulate 10 coin tosses.}
\item 解答：这是有放回的抽样，需要指定参数 \,{\color{blue}\verb+replace=T+}.
\begin{lstlisting}[language=R]
sample(c("H","T"), 10, replace=T)
\end{lstlisting}

\item  注：H = Head, T = Tail. 

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.1.b. 随机抽样}

\begin{itemize}
\item {\color{red}问题：设每次试验的成功概率为 0.9. 模拟10次试验的结果。}
\item 解答：需要指定概率分布，使用参数 \,{\color{blue}\verb+prob=c(0.9,0.1)+}.
\begin{lstlisting}[language=R]
sample(c("succ", "fail"), 10, replace=T, prob=c(0.9, 0.1))
\end{lstlisting}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.2.a. 古典概率 Probability calculations and combinatorics}

\begin{itemize}

\item  {\color{red}问题：在数集 $\{1,2,\cdots,40\}$ 中做不放回的抽样，选取5个数，并记录顺序。求某一给定序列发生的概率。}
\item 解答：概率为 $$\frac{1}{40\times 39\times 38\times 37\times 36}.$$ 
%计算命令为
\begin{lstlisting}[language=R]
1/prod(40:36)
\end{lstlisting}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.2.b. 古典概率 }

\begin{itemize}

\item  {\color{red}问题：40个数字里不放回地选5个，一共有多少种结果？求结果 $\{1,2,3,4,5\}$ 发生的概率。}

\item 解答：这两个问题的答案是互为倒数。

\begin{itemize}
\item  40个数字里不放回地选5个，所有可能的选法总数是组合数 \\
 $$\binom{40}{5} = \frac{40\times 39\times 38\times 37\times 36}{5\times 4\times 3\times 2\times 1}. $$
\item  前5个最小的数被选中的概率为 $$\frac{5\times 4\times 3\times 2\times 1}{40\times 39\times 38\times 37\times 36}. $$
\end{itemize}

\begin{lstlisting}[language=R]
choose(40,5)
prod(5:1)/prod(40:36)
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.3.a. 离散分布 Discrete distributions}

\begin{itemize}
\item {\color{red}问题：点概率和累计概率有什么差别？}

\item 解答：The {\color{blue}point probabilities} is $f(x) = \mathbb{P}(X = x)$, while the {\color{blue}cumulative distribution function} is $F(x) = \mathbb{P}(X\le x)$. 

\item 解答：点概率是指 $f(x) = \mathbb{P}(X = x)$, 累计概率是指 $F(x) = \mathbb{P}(X\le x)$. 


\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.3.b. 几何分布 }

\begin{itemize}

%\item {\color{red}问题：Let $X$ be the number of failures that occur before the first success. What is the distribution of $X$?}

\item {\color{red}问题：独立地持续进行同一种试验，设随机变量$X$ 是试验第一次成功时已经进行的试验次数。 求这个随机变量的分布。}


%\item 解答：It is the {\color{blue}geometric distribution}. Let $p$ be the probability of success in each trial, and let $q=1-p$. Then the distribution of $X$ is given by the following table.

\item 解答：随机变量 $X$ 服从几何分布。分布律如下表，其中 $p$ 是试验成功的概率，$q=1-p$ 是试验失败的概率。

\begin{center}
\begin{tabular}{|c|c|c|c|c|c|c|} \hline
$X=k$ & 1 & 2 & 3 & $\cdots$ & $n$ & $\cdots$ \\ \hline
概率 $\mathbb{P}\{X=k\}$ & $p$ & $qp$ & $q^2p$ & $\cdots$ & $q^{n-1}p$ & $\cdots$ \\ \hline 
\end{tabular}
\end{center}


\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.3.c. 二项分布 }

\begin{itemize}

\item  {\color{red}Q: What is the point distribution of the binomial distribution? }


\item 解答：设每次试验成功的概率是 $p$, 失败的概率是 $q=1-p$. 独立地将试验进行 $n$ 次，记 $X$ 是成功的次数。则随机变量 $X$ 服从二项分布，记为 $X\sim b(n,p)$. 分布律的公式形式和表格形式分别为
\[ f(x) = \binom{n}{x}p^x(1-p)^{n-x}, \,\, 0\le x\le n. \]
\begin{center}
\begin{tabular}{|c|c|c|c|c|c|c|c|} \hline
$X=x$ & 0 & 1 & 2 & $\cdots$ & $x$ & $\cdots$ & $n$  \\ \hline
概率 $\mathbb{P}\{X=x\}$ & $p^n$ & $\binom{n}{1}p^{n-1}q$ & $\binom{n}{2}p^{n-2}q^2$ & $\cdots$ & $\binom{n}{x}p^{n-x}q^x$ & $\cdots$ & $q^n$ \\ \hline 
\end{tabular}
\end{center}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.3.d. 二项分布的点概率（程序）}

\begin{itemize}

\item  问题：画出二项分布的点概率图。

\item 解答：使用 \,{\color{blue}\texttt{dbinom}} 函数计算二项分布的点概率。

\begin{lstlisting}[language=R]
n <- 10
p <- 0.4
x <- 0:n
y <- dbinom(x,n,p) #计算二项分布的分布律
plot(x,y,type='h',lwd=5,col='blue') #画出分布律的针状图
title('binomial distribution b(10,0.4)') #写图的标题
\end{lstlisting}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.3.e. 二项分布的点概率（图像）}

\begin{center}
\includegraphics[height=0.7\textheight, width=0.9\textwidth]{binomial-distribution-example.png}
\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.3.f. 用正态分布近似二项分布 }

\begin{itemize}
\item  {\color{red} 问题：如何用正态分布来近似二项分布？}
\item 解答：对一些 $(n,p)$ 的值，画出二项分布 $b(n,p)$ 的点概率图，估计它跟哪个正态分布比较接近。 
好像跟 $N(np,np(1-p))$ 比较接近。

%\item {\color{red} 问题：阅读材料，举一个正态近似的例子。}
%\url{http://statweb.stanford.edu/~susan/courses/s141/hoci.pdf}
%\item 解答：阅读并重复该课件里的代码。

\item  {\color{red} 问题：什么是 de Moivre - Laplace 中心极限定理？}
\item 解答：设 $X_n$ 服从二项分布 $b(n,p)$, 则当 $n$ 很大时，有
$$ \frac{X_n - np}{\sqrt{np(1-p)}} \,\,\dot{\sim}\,\, N(0,1). $$


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.3.g. 泊松分布 }

\begin{itemize}

\item  {\color{red} 问题：什么是泊松分布？}


\item 解答：泊松分布是二项分布 $b(n,p)$ 当 $n$ 趋于无穷大，而 $np$ 趋于常数 $\lambda$ 时的极限，记为 $X\sim pois(\lambda)$. 其分布律的公式形式和表格形式分别为
\[ f(k) = \frac{\lambda^k}{k!}e^{-\lambda}, \,\, 0\le k<\infty. \]
\begin{center}
\begin{tabular}{|c|c|c|c|c|c|c|} \hline
$X=k$ & 0 & 1 & 2 & $\cdots$ & $k$ & $\cdots$   \\ \hline
概率 $\mathbb{P}\{X=k\}$ & $e^{-\lambda}$ & $\lambda e^{-\lambda}$ & $\frac{\lambda^2}{2!}e^{-\lambda}$ & $\cdots$ & $\frac{\lambda^k}{k!}e^{-\lambda}$ & $\cdots$  \\ \hline 
\end{tabular}
\end{center}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.4.a. 连续分布 Continuous distributions}

\begin{itemize}
\item {\color{red} Question: How do you interpret the concept of the probability density? }

\item The {\color{blue}probability density} $f(x)$ is the infinitesimal probability of hitting a small region around $x$ divided by the size of the region. Its relation with the {\color{blue}cumulative distribution function} is that {\color{blue}{\it cdf = accumulated pdf}}, 
\[ \mathbb{P}(X\le x)=: F(x)=\int_{-\infty}^x f(x)dx. \] 

\end{itemize}

\begin{center}
\includegraphics[height=0.4\textheight, width=0.9\textwidth]{cumulative-distribution-example-2.png}
\end{center}

%http://www.alisonsinclair.ca/2011/03/shading-between-curves-in-r/
%x <- seq(-3,3,0.01)
%y1 <- dnorm(x,0,1)
%y2 <- 0.5*dnorm(x,0,1)
%plot(x,y1,type="l",bty="L",xlab="X",ylab="dnorm(X)")
%points(x,y2,type="l",col="red")
%polygon(c(x,rev(x)),c(y2,rev(y1)),col="skyblue")

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.4.b. 画出上一页的概率密度函数的图像 }

%\begin{itemize}
%\item {\color{red} Question: How did you draw the picture in the previous page? }
%\item Answer: I used R function \,{\color{blue}\verb+polygon()+} to shade an area. The first and second parameters of this function are the vectors of x-coordinates and y-coordinates.

%{\small\color{blue}
%\begin{verbatim}

\begin{lstlisting}[language=R]
x<-seq(-6,12,0.2)
ya<-dnorm(x,mean=0,sd=2)
yb<-dnorm(x,mean=6,sd=1.5)
y<-ya/2+yb/2 # in order to make two peaks
plot(x,y,type='l') # the type of the drawing is 'line'
abline(h=0) # draw the horizontal line, i.e., x-axis 
x1<-x[x<=2]
y1<-y[x<=2]
polygon(c(x1,tail(x1,1),head(x1,1)),c(y1,0,0),col='skyblue')
x2<-x[5<=x & x<=6]
y2<-y[5<=x & x<=6]
polygon(c(x2,tail(x2,1),head(x2,1)),c(y2,0,0),col='skyblue')
\end{lstlisting}

%\end{verbatim}
%}

%\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.4.c. 正态分布 }

\begin{itemize}

\item {\color{red} 问题：写出正态分布的概率密度函数。}
\item 解答：均值为 $\mu$, 方差为 $\sigma^2$ 的正态分布的概率密度函数为
\[ f(x) = \frac{1}{\sqrt{2\pi}\sigma} \exp\left( -\frac{(x-\mu)^2}{2\sigma^2} \right), \,\,\, -\infty<x<\infty.  \]

%\item {\color{red} 问题：画出正态分布的概率密度函数的图像。}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.4.d. 均匀分布 }

\begin{itemize}

\item {\color{red} 问题：写出均匀分布的概率密度函数。}
\item 解答：在区间 $[a,b]$ 上均匀分布的随机变量的概率密度函数为
\begin{align*} 
f(x) = \left\{\begin{array}{ll}
\frac{1}{b-a}, & a\le x \le b, \\
0, & x<a \text{ 或 } x>b.
\end{array}\right.
\end{align*}

%\item {\color{red} 问题：画出均匀分布的概率密度函数的图像。}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.4.e. 指数分布 }

\begin{itemize}

\item {\color{red} 问题：写出指数分布的概率密度函数。}
\item 解答：均值为 $\frac{1}{\lambda}$ 的指数分布的随机变量的概率密度函数为
\begin{align*} 
f(x) = \left\{\begin{array}{ll}
\lambda e^{-\lambda x}, & x \ge 0, \\
0, & x<0.
\end{array}\right.
\end{align*}

%\item {\color{red} 问题：画出指数分布的概率密度函数的图像。}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.4.f. $\chi^2$ 分布 }

\begin{itemize}

\item {\color{red} 问题：什么是 $\chi^2$ 分布？}
\item 解答：称随机变量 $K$ 服从自由度为 $n$ 的 $\chi^2$ 分布，是指
\begin{align*} 
K = Y_1^2+\cdots+Y_n^2,
\end{align*}
其中 $Y_1,\cdots,Y_n$ 是相互独立的标准正态分布的随机变量。记为 $$K \sim \chi^2(n). $$ 

%\item {\color{red} 问题：画出指数分布的概率密度函数的图像。}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.4.g. t 分布 }

\begin{itemize}

\item {\color{red} 问题：什么是 t 分布？}
\item 解答：称随机变量 $T$ 服从自由度为 $n$ 的 t 分布，是指
\begin{align*} 
T = \frac{X}{\sqrt{(Y_1^2+\cdots+Y_n^2)/n}},
\end{align*}
其中 $X,Y_1,\cdots,Y_n$ 是相互独立的标准正态分布的随机变量。记为 $$T\sim t(n). $$ 

%\item {\color{red} 问题：画出指数分布的概率密度函数的图像。}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.4.h. F 分布 }

\begin{itemize}

\item {\color{red} 问题：什么是 F 分布？}
\item 解答：称随机变量 $F$ 服从自由度为 $(m,n)$ 的 F 分布，是指
\begin{align*} 
F = \frac{(X_1^2+\cdots+X_m^2)/m}{(Y_1^2+\cdots+Y_n^2)/n},
\end{align*}
其中 $X_1,\cdots,X_m, Y_1,\cdots,Y_n$ 是相互独立的标准正态分布的随机变量。记为 $$F\sim F(m,n). $$ 

%\item {\color{red} 问题：画出指数分布的概率密度函数的图像。}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5. 内建函数 The built-in distributions in R}

\begin{itemize}
\item  {\color{red} 问题：说明R软件中内建的与概率分布有关的四种函数。}
\item 解答：Four items can be calculated for a statistical distribution:

\begin{enumerate}
\item 概率密度或点概率：Density or point probability
\item （累计）分布函数：Cumulated probability, distribution function 
\item 分位数：Quantiles
\item 伪随机数：Pseudo-random numbers
\end{enumerate}

\item  {\color{red} 问题：以正态分布为例，写出上述四种函数的使用方法。}
\item 解答：函数名分别是 \,{\color{blue}\verb+dnorm+}, \,{\color{blue}\verb+pnorm+}, \,{\color{blue}\verb+qnorm+}, 和 \,{\color{blue}\verb+rnorm+}. 

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.1.a. 概率密度的概念 Concept of pdf}

\begin{itemize}
\item  {\color{red} 问题：解释连续型随机变量的概率密度的含义。}
\item 解答：The {\color{blue}density} for a continuous distribution is a measure of the relative probability of ``getting a value close to $x$''. {\color{blue}The probability} of getting a value in a particular interval {\color{blue}is the area} under the corresponding part of the curve.


\item  {\color{red} 问题：解释离散型随机变量的概率密度的含义。}
\item 解答：For discrete distributions, the term {\color{blue}density} is used for the point probability -- the probability of getting exactly the value $x$. Technically, this is correct: It is a density with respect to {\color{blue}counting measure}.

\item  Notebook: pdf = probability density function.

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.1.b. 概率密度的例子：正态分布 }

\begin{itemize}
\item {\color{red} 问题：画出标准正态分布的密度函数的图像。}

\item 解答一：画出横坐标等距的一些点，然后使用 \,{\color{red}\verb+plot()+} 函数连线。
\begin{lstlisting}[language=R]
x <- seq(-4,4,0.1)
plot(x,dnorm(x),type="l")
\end{lstlisting}

\item 解答二：使用 \,{\color{red}\verb+curve()+} 函数，第一个参数是函数表达式。
\begin{lstlisting}[language=R]
curve(dnorm(x), from=-4, to=4)
\end{lstlisting}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.1.c. 标准正态分布的概率密度函数 }

\begin{center}
\includegraphics[height=0.4\textheight, width=0.7\textwidth]{plot-3-5-1-2.png}
\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.1.d. 点概率的例子：二项分布 }

\begin{itemize}

\item {\color{red} 问题：画出二项分布 $b(50,0.33)$ 的点概率图（针形图）。}
\item 解答：在 \,{\color{red}\verb+plot()+} 函数中参数设置使用  \,{\color{red}\verb+type='h'+}. 
\begin{lstlisting}[language=R]
x=0:50
y=dbinom(x,50,0.33)
plot(x,y,type='h')
\end{lstlisting}

\begin{center}
\includegraphics[height=0.4\textheight, width=0.7\textwidth]{plot-3-5-1-2-b.png}
\end{center}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.2.a. 累计分布函数的概念 Concept of cdf }

\begin{itemize}
\item  {\color{red} Q: What is the cumulative distribution function (cdf)?}
\item 解答：The {\color{blue} cumulative distribution function} describes the probability of ``hitting'' $x$ or less in a given distribution, i.e.,  
\[ F(x) := \mathbb{P} (X\le x), \,\, -\infty < x < \infty.  \]

\item  {\color{red} 概率密度函数 pdf 与累计分布函数 cdf 的联系是什么？}
\item 解答：对连续型随机变量，设 $f(x)$ 是密度函数，则有下述第二个等式 
\[ F(x) = \int_{-\infty}^{x} dF(x) = \int_{-\infty}^{x} f(x)dx. \]
\item 注解：上式的第一个等号总是成立的，其中的积分理解为 R-S 积分。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.2.b. 分布函数的例子：特定人口的数量比例 }

\begin{itemize}
\item {\color{red} Q: Suppose that some biochemical measure in healthy individuals is well described by a normal distribution with a mean of 132 and a standard deviation of 13. Now a patient has a value of 160. 
What is the percentage of the general population that has this value or higher. }

%Then, if a patient has a value of 160, there is or only about of the general population, that has that value or higher.

\item 解答：设 $X$ 是正态分布随机变量，服从 $N(132,13^2)$. 则 
\[ \mathbb{P}(X\ge 160) = 1 - \mathbb{P}(X < 160) = 1 - \mathbb{P}(X \le 160) = 0.01563. \]

\item 代码：使用 \,{\color{blue}\texttt{pnorm()}} 函数，并设置均值与标准差这两个参数。
\begin{lstlisting}[language=R]
1-pnorm(160,mean=132,sd=13)
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.2.c. 正态分布 $N(132,13^2)$ 的分布函数的图像 }

\begin{center}
\includegraphics[height=0.7\textheight, width=0.9\textwidth]{plot-3-5-2-3.png}
\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.2.d. 正态分布 $N(132,13^2)$ 的密度函数的图像}

\begin{center}
\includegraphics[height=0.7\textheight, width=0.9\textwidth]{plot-3-5-2-4.png}
\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.2.e. 二项分布的例子：假设检验 }

\begin{itemize}
\item  {\color{red} Q: Twenty patients are given two treatments each and then asked whether treatment A or B worked better. It turned out that 16 patients liked A better. Whether this can be taken as sufficient evidence that A actually is the better treatment? }
%or whether the outcome might as well have happened by chance even if the treatments were equally good?

\vspace{0.4cm}

\item 解答：设随机一个人喜欢治疗方法A的概率为 $p$, 则喜欢治疗方法A的总人数 $X$ 服从二项分布 $b(20,p)$. 考虑假设检验问题：
\[ H_0: p\le 0.5, \,\,v.s.\,\, H_1: p>0.5. \]
如果在零假设成立的条件下，概率 $\mathbb{P}(X\ge 16)$ 是个小概率事件，那么要拒绝这个零假设，并认为治疗方法A确实更有效。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.2.f. 二项分布的例子：假设检验 }

\begin{itemize}

\item 记 $X\sim b(20,p)$, 为计算方便，考虑随机变量 $Y\sim b(20,0.5)$. 根据零假设，$p\le 0.5$, 从而可以估计该假设检验问题的 $p$ 值 
\begin{eqnarray*}
p=\mathbb{P}(X\ge 16) \le \mathbb{P}(Y\ge 16)=0.005908966. 
\end{eqnarray*}
因为这个 $p$ 值小于显著性水平，所以拒绝零假设。

\item 代码：使用 \,{\color{red}\verb+pbinom()+} 函数，设置参数 $(n,p)=(20,0.5)$. %并设置试验次数与每次试验中事件发生的概率这两个参数。
\begin{lstlisting}[language=R]
1 - pbinom(15, size=20, prob=.5)
x <- 0:20
y1 <- dbinom(x,20,0.4)
y2 <- dbinom(x,20,0.5)
sum(y1[x>=16]) # [1] 0.0003170311 # 可以看到这个概率更小
sum(y2[x>=16]) # [1] 0.005908966 
\end{lstlisting}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.2.g. 二项分布b(20,0.5)的点概率图 }

\begin{center}
\includegraphics[height=0.7\textheight, width=0.9\textwidth]{plot-3-5-2-7.png}
\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.2.h. 二项分布b(20,0.4)的点概率图 }

\begin{center}
\includegraphics[height=0.7\textheight, width=0.9\textwidth]{plot-3-5-2-8.png}
\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.3.a. 分位数的概念 Concept of quantiles}

\begin{itemize}

\item {\color{red} 问题：什么是分位数？}

\item 解答：The {\color{blue}quantile function $x=F^{-1}(p)$} is the inverse of the cumulative distribution function $p=F(x) := \mathbb{P} (X\le x)$. The $p$-quantile is the value with the property that there is probability $p$ of getting a value less than or equal to it. The median is by definition the 50\% quantile.

\item 解答：分位数 $x=F^{-1}(p)$ 是累计分布函数 $p=F(x)$ 的反函数。某个分布的 $p$ 分位数，是指符合条件{\color{blue}“取值小于等于 $x$ 的概率等于 $p$”}的实数 $x$. 按这个定义，中位数正好是 $0.5$ 分位数。


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.3.b. 分位数的概念 Concept of quantiles}

\begin{itemize}

\item {\color{red} 问题：计算标准正态分布的 0.025 分位数和 0.975 分位数。}
\item 解答：使用 \,{\color{red}\verb+qnorm()+} 函数。
\begin{lstlisting}[language=R]
qnorm(0.025) 
#[1] -1.959964  
#在pdf图像中，这个数左边的阴影面积是0.025. 

qnorm(0.975) 
#[1] 1.959964  
#在pdf图像中，这个数左边的阴影面积是0.975. 
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.3.c. 分位数的概念 }

\begin{center}
\includegraphics[height=0.7\textheight, width=0.9\textwidth]{plot-3-5-3-2.png}
\end{center}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.3.d. 分位数的例子 Examples of quantiles}

\begin{itemize}

\item {\color{red} Q: If we have $n$ normally distributed observations with the same mean $\mu$ and standard deviation $\sigma$, then it is known that the average $\overline{x}$ is normally distributed around $\mu$ with standard deviation $\sigma/\sqrt{n}$. Find the 95\% confidence interval for $\mu$. }

\item 解答： 设总体 $X\sim N(\mu,\sigma^2)$, 设简单随机样本 $X_1,X_2,\cdots,X_n$. 样本均值
\[ \overline{X} = \frac{1}{n}\left( X_1 + X_2 + \cdots + X_n\right) \sim N(\mu,\sigma^2/n). \]
记 $N_{0.025}$ 和 $N_{0.975}$ 分别是 $0.025$ 分位数和 $0.975$ 分位数，即
\[ \mathbb{P}\left(\frac{\overline{X}-\mu}{\sigma/\sqrt{n}}\le N_{0.025}\right) = 0.025, \,\, 
\mathbb{P}\left( \frac{\overline{X}-\mu}{\sigma/\sqrt{n}} \le N_{0.975}\right) = 0.975. \]
从这两个不等式就得到参数 $\mu$ 的 95\% 置信区间。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.4.a. 随机数 Random numbers}

\begin{itemize}
\item {\color{red} 问题：生成10个标准正态分布的随机数。}
\item 解答： \,{\color{blue}\verb+> rnorm(10)+} 

\item  {\color{red} 生成10个正态分布 $N(7,5^2)$ 的随机数。}
\item 解答：\,{\color{blue}\verb+> rnorm(10,mean=7,sd=5)+} 

\item {\color{red} 问题：生成10个在区间 $[3,5]$ 上均匀分布的随机数。}
\item 解答：\,{\color{blue}\verb+> runif(10,3,5)+} 



\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.4.b. 随机行走 Random walk}

\begin{itemize}
\item  问题：模拟一维随机行走。

\item  解答：设每步行走距离服从区间 $[-3,3]$ 上的均匀分布。

\begin{lstlisting}[language=R]
N <- 30
x <- 0:N
y <- runif(N,-3,3)
y <- c(0,y)
plot(x,cumsum(y),xlim=c(0,N+1),ylim=c(-10,10),type='o')
abline(h=0)
abline(v=0)
\end{lstlisting}

\item  注：横坐标是时间，随机行走发生在纵坐标上。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.5.4.c. 随机行走 Random walk }

\begin{center}
\includegraphics[height=0.8\textheight, width=0.9\textwidth]{random-walk-example-2.png}
\end{center}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.6.1. Exercise.   }

\begin{itemize}
\item  Question. Calculate the probability for each of the following events: 
\begin{enumerate}
\item[(a)] A standard normally distributed variable is larger than 3. 
\item[(b)] A normally distributed variable with mean 35 and standard deviation 6 is larger than 42. 
\item[(c)] Getting 10 out of 10 successes in a binomial distribution with probability 0.8. 
\item[(d)] $X < 0.9$ when $X$ has the standard uniform distribution. 
\item[(e)] $X > 6.5$ in a $\chi^2$ distribution with 2 degrees of freedom.
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.6.1. Exercise - Answer.   }

\begin{itemize}
\item  Answer. 
\begin{enumerate}
\item[(a)] \,{\color{blue}\texttt{1 - pnorm(3)}}
\item[(b)] \,{\color{blue}\texttt{1 - pnorm(42, mean=35, sd=6)}}
\item[(c)] \,{\color{blue}\texttt{dbinom(10, size=10, prob=0.8)}}
\item[(d)] \,{\color{blue}\texttt{punif(0.9)}} \# this one is obvious...
\item[(e)] \,{\color{blue}\texttt{1 - pchisq(6.5, df=2)}}
\end{enumerate}

\item  Notebook. 
It might be better to use \,{\color{blue}\texttt{lower.tail=FALSE}} instead of subtracting from 1 in (a), (b), and (e). Notice that question (c) is about a point probability, whereas the others involve the cumulative distribution function.

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.6.1. 单项选择题   }

\begin{itemize}
\item  问题：下述计算中，不正确的是哪个？
\begin{enumerate}
\item[(a)] 一个标准正态分布的随机变量，取值大于3的概率大约是 $0.001350$. 
\item[(b)] 一个均值为 35 标准差为 6 的正态分布的随机变量，取值大于42的概率大约是 $0.1217$. 
\item[(c)] 服从二项分布 $b(10,0.8)$ 的随机变量取值等于10的概率大约是 $0.1074$. 
\item[(d)] 一个标准均匀分布的随机变量取值小于 0.9  的概率是0.1.  
\item[(e)] 一个自由度为2的 $\chi^2$ 分布的随机变量，取值大于 6.5 的概率大约是 $0.03877$. 
\end{enumerate}



\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.6.1. 单项选择题   }

\begin{itemize}
\item  解答：(d). 标准均匀分布是指区间 $[0,1]$ 上的均匀分布。


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.6.2. Exercise.   }

\begin{itemize}
\item  Question. A {\color{blue}rule of thumb} is that 5\% of the normal distribution lies outside an interval approximately $\pm 2s$ about the mean. 
\begin{enumerate}
\item  To what extent is this true? 
\item  Where are the limits corresponding to 1\%, 0.5\%, and 0.1\%? 
\item  What is the position of the quartiles measured in standard deviation units?
\end{enumerate}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.6.2. Exercise - Answer.   }

\begin{itemize}

\item  Answer. 
Evaluate each of the following. Notice that the standard normal can be used for all questions.

\begin{lstlisting}[language=R]
pnorm(-2) * 2
qnorm(1-.01/2)
qnorm(1-.005/2)
qnorm(1-.001/2)
qnorm(.25)
qnorm(.75)
\end{lstlisting}

\item  Notebook. Again, \,{\color{blue}\texttt{lower.tail}} can be used in some cases.

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.6.2. 单项选择题   }

\begin{itemize}
\item  问题：设随机变量 $X$ 服从标准正态分布。 设分位数 $c$ 使得 $$\mathbb{P}(-c \le X\le c) = 0.95. $$ 则 $c$ 等于多少？
\begin{enumerate}[(a)]
\item  1.65. 
\item  1.96. 
\item  2.33. 
\item  2.58. 
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.6.2. 单项选择题   }

\begin{itemize}

\item  解答：(b) $c=1.96$. 
\begin{lstlisting}[language=R]
qnorm(.025)
qnorm(.975)
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.6.3. Exercise.   }

\begin{itemize}
\item  Question. For a disease known to have a postoperative complication frequency of 20\%, a surgeon suggests a new procedure. He tests it on 10 patients and there are no complications. What is the probability of operating on 10 patients successfully with the traditional method?

\begin{enumerate}[(a)]
\item  0.1074.
\item  0.1174.
\item  0.1274.
\item  0.1374.
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.6.3. Exercise - Answer.   }

\begin{itemize}

\item  Answer. (a). The probability is $0.1074$. 

\begin{lstlisting}[language=R]
dbinom(0, size=10, prob=.2)
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.6.4. Exercise.   }

\begin{itemize}
\item  Question. Simulated coin-tossing can be done using \,{\color{blue}\texttt{rbinom}} instead of \,{\color{blue}\texttt{sample}}. How exactly would you do that?

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.6.4. Exercise - Answer.   }

\begin{itemize}
\item  Answer. 
Either of the following should work:

\begin{lstlisting}[language=R]
rbinom(10, 1, .5)
ifelse(rbinom(10, 1, .5) == 1, "H", "T")
c("H", "T")[1 + rbinom(10, 1, .5)]
\end{lstlisting}

\item  Notebook. The first one gives a 0/1 result, the two others H/T like the \,{\color{blue}\texttt{sample}} example in the text. One advantage of using \,{\color{blue}\texttt{rbinom}} is that its \,{\color{blue}\texttt{prob}} argument can be a vector, so you can have different probabilities of success for each element of the result.

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.6.4. 单项选择题   }

\begin{itemize}
\item  问题：使用 \,{\color{blue}\texttt{rbinom}} 模拟投掷硬币。下述代码不正确的是哪个？
\begin{enumerate}[(a)]
\item  \,{\color{blue}\texttt{rbinom(10, 1, .5) }}
\item  \,{\color{blue}\texttt{ifelse(rbinom(10, 1, .5) == 1, "H", "T") }}
\item  \,{\color{blue}\texttt{c("H", "T")[1 + rbinom(10, 1, .5)] }}
\item  \,{\color{blue}\texttt{rbinom(10, c("H", "T")) }}
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.6.4. 单项选择题   }

\begin{itemize}

\item  解答：(d). 阅读 \,{\color{blue}\texttt{rbinom}} 的使用手册。

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.1. 单项选择题   }

\begin{itemize}

\item 问题：  %1
从1-10随机选取3个数，不能重复选取，下述哪个命令可以实现？
\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ choose(10,3) +}
\item  {\color{blue}\verb+ sample(3, 1:10) +}
\item  {\color{blue}\verb+ sample(1:10, 3, replace=T) +}
\item  {\color{blue}\verb+ sample(1:10, 3) +}
\end{enumerate}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.1. 单项选择题   }

\begin{itemize}

\item %1

解答：(d).
选项(a)是计算组合数。选项(b)参数位置不对。选项(c)是有放回的抽样。


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.2. 单项选择题   }

\begin{itemize}

\item 问题：  %2
按照下述概率分布，产生10个随机数。下述哪个命令可以实现？
\begin{center}
\begin{tabular}{|c|c|c|c|} \hline
$X$ & 1 & 2 & 3 \\ \hline
$p_k$ & 0.2 & 0.3 & 0.5 \\ \hline
\end{tabular}
\end{center}

\begin{enumerate}[(a)]
\item  {\color{blue}\verb+ sample(1:3, replace=T, prob=c(0.2,0.3,0.5)) +}
\item  {\color{blue}\verb+ sample(1:3, 10, r=T, p=c(0.2,0.3,0.5)) +}
\item  {\color{blue}\verb+ choose(1:3, replace=T, prob=c(0.2,0.3,0.5)) +}
\item  {\color{blue}\verb+ choose(1:3, 10, r=T, p=c(0.2,0.3,0.5)) +}
\end{enumerate}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.2. 单项选择题   }

\begin{itemize}

\item %2
解答：(b).
选项(a)的结果是3个数的全排列。(c)和(d)的函数 {\color{blue}\verb+ choose +} 是用来计算组合数的。


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.3. 单项选择题   }

\begin{itemize}

\item 问题：  %3
运行下述两行程序。下述说法中，不正确的是哪个？
\begin{lstlisting}[language=R]
x <- prod(4:2) + sum(4:2) + choose(4,2)
y <- 4*3*2 + (4+3+2) + 4*3/2/1
\end{lstlisting}

\begin{enumerate}[(a)]
\item  结果变量 {\color{blue}\verb+ x +} 的值是 39. 
\item  结果变量 {\color{blue}\verb+ y +} 的值是 39. 
\item  函数 {\color{blue}\verb+ prod +} 和 {\color{blue}\verb+ sum +} 分别计算一个向量的所有元素的连乘和连加。
\item  函数 {\color{blue}\verb+ choose(n,k) +} 计算从 $n$ 个数里选出 $k$ 个数的排列数。
\end{enumerate}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.3. 单项选择题   }

\begin{itemize}

\item %3
解答：(d).
选项 (d) 是不对的，这个函数 {\color{blue}\verb+ choose(n,k) +} 计算从 $n$ 个数里选出 $k$ 个数的组合数。差别就是组合数不需要有顺序，因此要除以 $k!$. 



\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.4. 单项选择题   }

\begin{itemize}

\item 问题：  %4
设每次试验成功的概率是 $0.3$. 独立地重复这个试验10次。恰好有3次成功的概率是多少？ 

\begin{enumerate}[(a)]
\item  0.2468
\item  0.2568
\item  0.2668
\item  0.2768
\end{enumerate}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.4. 单项选择题   }

\begin{itemize}

\item %4
解答：(c).
使用下述命令即得答案。
\begin{lstlisting}[language=R]
dbinom(3,10,0.3)
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.5. 单项选择题   }

\begin{itemize}

\item 问题：  %5
设每次试验成功的概率是 $0.3$. 独立地重复这个试验10次。至少有3次成功的概率是多少？ 

\begin{enumerate}[(a)]
\item  0.6172
\item  0.6272
\item  0.6372
\item  0.6472
\end{enumerate}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.5. 单项选择题   }

\begin{itemize}

\item %5
解答：(a).
设 $X\sim b(n,p)$. 这个题目是要计算 $\mathbb{P}(X\ge 3)$. 
方法一是转化成累计概率密度函数，可得
$$\mathbb{P}(X\ge 3) = 1 - \mathbb{P}(X\le 2).$$
方法二是把所有符合的情况的概率加起来，也就是
$$\mathbb{P}(X\ge 3) = \sum\limits_{k=3}^{10} \mathbb{P}(X=k). $$
因此下述两个命令都可以：
\begin{lstlisting}[language=R]
1 - pbinom(3,10,0.3) 
sum(dbinom(3:10,10,0.3))
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.6. 单项选择题   }

\begin{itemize}

\item 问题：  %6
运行下述程序，选择不正确的那个说法。
\begin{lstlisting}[language=R]
x <- seq(-3,3,0.2)
y <- dnorm(x,mean=1,sd=1)
plot(x,y)
\end{lstlisting}

\begin{enumerate}[(a)]
\item  变量 {\color{blue}\verb+ x +} 是区间 $[-3,3]$ 上间距为 0.2 的等差数组。
\item  变量 {\color{blue}\verb+ y +} 保存了一个正态分布在自变量取值为 {\color{blue}\verb+ x +} 的概率密度函数值。
\item  图像最后画出一条正态分布的曲线。
\item  这个正态分布的均值是1，方差也是1.
\end{enumerate}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.6. 单项选择题   }

\begin{itemize}

\item  %6
解答：(c). 
由于没有指定参数 {\color{blue}\verb+ type='l'+}, 图像是一些点，没有连成曲线。


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.7. 单项选择题   }

\begin{itemize}

\item 问题：  %7
设某个地区的人均收入服从正态分布，均值为7万元，标准差为2万元。假设收入最低的5\%人口为贫困，请问这样定义的贫困线是多少？
\begin{enumerate}[(a)]
\item  1.7102万元
\item  2.7102万元
\item  3.7102万元
\item  4.7102万元
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.7. 单项选择题   }

\begin{itemize}

\item %7
解答：(c).
设随机变量 $X\sim N(7,2^2)$. 设这样定义的贫困线为 $c$, 则有 $$\mathbb{P}(X\le c)=0.05. $$
于是 $c$ 是 $X$ 的下 0.05 分位数。使用下述命令即得答案。 
\begin{lstlisting}[language=R]
qnorm(0.05,mean=7,sd=2)
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.8. 单项选择题   }

\begin{itemize}

\item 问题：  %8
设随机变量 $X\sim \chi^2(9)$, 即自由度为 9 的卡方分布。求概率 $$\mathbb{P}(X>20). $$
\begin{enumerate}[(a)]
\item  0.01691
\item  0.01791
\item  0.01891
\item  0.01991
\end{enumerate}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.8. 单项选择题   }

\begin{itemize}

\item %8
解答：(b).
根据卡方分布的定义，这个随机变量 $X$ 是9个相互独立的标准正态分布的随机变量的平方和。
使用累计分布函数，转化成计算 $$1-\mathbb{P}(X\le 20). $$
\begin{lstlisting}[language=R]
1 - pchisq(20,9)
\end{lstlisting}



\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.9. 单项选择题   }

\begin{itemize}

\item 问题：  %9
设 $X$ 服从标准正态分布。求实数 $c$ 使得 $$\mathbb{P}(|X|\ge c)=0.01. $$

\begin{enumerate}[(a)]
\item  2.4758
\item  2.5758
\item  2.6758
\item  2.7758
\end{enumerate}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.9. 单项选择题   }

\begin{itemize}

\item  %9
解答：(b).
根据对称性，这个问题等价于求分位数 $c$ 使得 $$\mathbb{P}(X\ge c)=0.005. $$ 为使用累计分布函数，将这个等式转化成
$$\mathbb{P}(X\le c)=0.995.$$ 使用分位数函数，也就是累积分布函数的逆函数，即得。
\begin{lstlisting}[language=R]
qnorm(0.995)
\end{lstlisting}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.10. 单项选择题   }

\begin{itemize}

\item 问题：  %10
设某时刻某地铁线路上的乘客人数服从泊松分布，均值是200人。求乘客人数超过220的概率。

\begin{enumerate}[(a)]
\item  0.05530
\item  0.06530
\item  0.07530
\item  0.08530
\end{enumerate}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.7.10. 单项选择题   }

\begin{itemize}

\item %10
解答：(c).
设 $X$ 是乘客人数。要计算概率 $$\mathbb{P}(X>220). $$ 为使用累计分布函数，转化成计算 $$1-\mathbb{P}(X\le 220). $$
\begin{lstlisting}[language=R]
1 - ppois(220,200)
\end{lstlisting}


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.8.1. 简答题   }

\begin{enumerate}

\item 模拟从1到45中随机取出3个数。分有放回和不放回的情形。

\item  举例说明 sample() 函数的使用方法，解释参数的含义。

\item 将班级的50位同学按照3位或4位一组的方式分组，共有多少种可能？

\item 用 \verb+sample()+ 函数得出10个服从下述分布的随机数：
\begin{table}[ht]\centering
\begin{tabular}{|c|c|c|c|c|}
\hline
$X$ & 1 & 2 & 3 & 4 \\
\hline
概率  & 0.1 & 0.2 & 0.3 & 0.4 \\
\hline
\end{tabular}
\end{table}

\item 用R语言编程计算 $1+1+ \frac{1}{2!}+ \frac{1}{3!}+\cdots+\frac{1}{20!} $ 的值。

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.8.6. 简答题   }

\begin{enumerate}\setcounter{enumi}{5}

\item  什么是分位数？计算标准正态分布的 $0.1, 0.05, 0.025$ 分位数。

\item  标准正态分布的四分位数分别是什么？十分位数分别是什么？即计算标准正态分布的 $0.25, 0.5, 0.75$ 分位数和 $0.1,0.2,\cdots,0.9$ 分位数。

\item  举例说明使用 R 语言生成复合给定分布的随机数。分别使用 rbinom() 函数和 sample() 函数模拟投币试验。

\item 设随机变量服从下述分布，画出概率图或密度函数图。
\begin{enumerate}
\item 二项分布 $b(45,0.2)$. 
\item 泊松分布 $P(5)$. 
\item 正态分布 $N(60,10^2)$. 
\item 指数分布 $Exp(5)$. 
\end{enumerate}

\item 查资料找到普通人的血压的分布。计算血压高于150的概率。


\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{3.8.11. 简答题   }

\begin{enumerate}\setcounter{enumi}{10}


\item  计算下述事件的概率：
\begin{enumerate}
\item  一个正态分布 $N(132,169)$ 的随机变量，取值大于等于 160 的概率。
\item  一个参数为 $(20,0.5)$ 的二项分布的随机变量，取值大雨等于 16 的概率。

%\item  一个标准正态分布的随机变量，取值大于 3 的概率。
\item  一个标准正态分布的随机变量，取值大于 $1.5$ 的概率。
%\item  一个均值为 35, 标准差为 6 的正态分布的随机变量，取值大于 42 的概率。
\item  一个均值为 30, 标准差为 5 的正态分布的随机变量，取值大于 40 的概率。
%\item  一个参数为 $(10,0.8)$ 的随机变量，取值等于 10 的概率。
\item  一个参数为 $(12,0.6)$ 的二项分布的随机变量，取值等于 8 的概率。
%\item  一个标准均匀分布的随机变量，取值小于 $0.9$ 的概率。
\item  一个标准均匀分布的随机变量，取值小于 $0.6$ 的概率。
%\item  一个自由度为 $2$ 的卡房分布的随机变量，取值大于 $6.5$ 的概率。
\item  一个自由度为 $5$ 的卡方分布的随机变量，取值大于 $5$ 的概率。
\end{enumerate}

\item  对于一种疾病，已知术后并发症的发生频率为 15\%. 一位外科医生建议了一种新的方法，测试了12位病人，都没有并发症。按照往常的并发症的发生频率，12位病人全部手术成功的概率是多少？

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}



